<?php

namespace App\Imports;

use App\Http\Models\Business\InvoiceDetails;
use App\Http\Models\DistributionChannel;
use App\Http\Models\ProjectType;
use App\Http\Models\SalesOffice;
use App\Http\Models\SalesOrderType;
use App\Http\Models\SalesOrganization;
use App\Http\Models\SalesRegion;
use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class InvoiceDetailsImport implements ToCollection, WithHeadingRow
{
    use BaseImportTrait;

    private $required_field = [
        '年份', '月份', '销售订单号', '销售订单行号', '物料号', '开票数量',
        '开票总价(含税)(折扣前)', '开票总价(含税)', '开票总价(不含税)', '税额', '折扣金额', '附加费',
        '销售订单类型', '项目类型', '销售组织', '销售大区', '销售办事处', '分销渠道', '付款方', '业务员'
    ];

    private $sales_order_type_array = [];
    private $project_type_array = [];
    private $sales_organization_array = [];
    private $sales_region_array = [];
    private $sales_office_array = [];
    private $distribution_channel_array = [];


    public function collection(Collection $rows)
    {
        foreach ($rows as $key => $row) {
            $row = $row->toArray();

            if ($key === 0) {
                $this->verifyField($row);
                $this->prepareBaseData();
            }

            $this->verifyNotNull($row, $key + 2);
            $this->verifyValueExist($row, $key + 2);

            $this->saveData($row);
        }
    }

    //准备基础数据，用来验证
    private function prepareBaseData()
    {
        $this->sales_order_type_array = SalesOrderType::pluck('code')->toArray();
        $this->project_type_array = ProjectType::pluck('code')->toArray();
        $this->sales_organization_array = SalesOrganization::pluck('code')->toArray();
        $this->sales_region_array = SalesRegion::pluck('code')->toArray();
        $this->sales_office_array = SalesOffice::pluck('code')->toArray();
        $this->distribution_channel_array = DistributionChannel::pluck('code')->toArray();
    }


    //验证字段值存在于数据库基础表中
    private function verifyValueExist(array $row, int $index)
    {
        if (!in_array($row['销售订单类型'], $this->sales_order_type_array)) {
            $this->no_error = false;
            $this->error_field = '销售订单类型';
        } elseif (!in_array($row['项目类型'], $this->project_type_array)) {
            $this->no_error = false;
            $this->rror_field = '项目类型';
        } elseif (!in_array($row['销售组织'], $this->sales_organization_array)) {
            $this->no_error = false;
            $this->error_field = '销售组织';
        } elseif (!in_array($row['销售大区'], $this->sales_region_array)) {
            $this->no_error = false;
            $this->error_field = '销售大区';
        } elseif (!in_array($row['销售办事处'], $this->sales_office_array)) {
            $this->no_error = false;
            $this->error_field = '销售办事处';
        } elseif (!in_array($row['分销渠道'], $this->distribution_channel_array)) {
            $this->no_error = false;
            $this->error_field = '分销渠道';
        }

        if (!$this->no_error) {
            throw new Exception('第 ' . $index . ' 行中的 [' . $this->error_field . '] 的值不存在于系统中！');
        }
    }

    //保存数据
    private function saveData(array $row)
    {
        $model = new InvoiceDetails();
        $model->year = $row['年份'];
        $model->month = $row['月份'];
        $model->sales_order_no = $row['销售订单号'];
        $model->sales_order_line_no = $row['销售订单行号'];
        $model->material = $row['物料号'];
        $model->quantity = $row['开票数量'];
        $model->total_price_tax_discount = $row['开票总价(含税)(折扣前)'];
        $model->total_price_tax = $row['开票总价(含税)'];
        $model->total_price_no_tax = $row['开票总价(不含税)'];
        $model->tax = $row['税额'];
        $model->discount = $row['折扣金额'];
        $model->surcharge = $row['附加费'];
        $model->sales_order_type_code = $row['销售订单类型'];
        $model->project_type_code = $row['项目类型'];
        $model->sales_organization_code = $row['销售组织'];
        $model->sales_region_code = $row['销售大区'];
        $model->sales_office_code = $row['销售办事处'];
        $model->distribution_channel_code = $row['分销渠道'];
        $model->payer_code = $row['付款方'];
        $model->salesman_id = $row['业务员'];

        $model->save();
    }
}
